import { message } from "ant-design-vue";
interface CheckCondition {
    format?: string[];
    // 使用多少 M 为单位
    size?: number;
}
type ErrorType = "size" | "format" | null;
// 上传组件检查公共逻辑
export function beforeUploadCheck(file: File, condition: CheckCondition) {
    const { format, size } = condition;
    const isValidFormat = format ? format.includes(file.type) : true;
    const isValidSize = size ? file.size / 1024 / 1024 < size : true;
    let error: ErrorType = null;
    if (!isValidFormat) {
        error = "format";
    }
    if (!isValidSize) {
        error = "size";
    }
    return {
        passed: isValidFormat && isValidSize,
        error,
    };
}

// 检查逻辑
export const commonUploadCheck = (file: File) => {
    const result = beforeUploadCheck(file, { format: ["image/jpeg", "image/png"], size: 1 });
    const { passed, error } = result;
    if (error === "format") {
        message.error("上传图片只能是 JPG/PNG 格式!");
    }
    if (error === "size") {
        message.error("上传图片大小不能超过 1Mb");
    }
    return passed;
};
// 获取图片大小信息
export const getImageDimensions = (url: string | File) => {
    return new Promise<{ width: number; height: number }>((resolve, reject) => {
        const img = new Image();
        img.src = typeof url === "string" ? url : URL.createObjectURL(url);
        img.addEventListener("load", () => {
            const { naturalWidth: width, naturalHeight: height } = img;
            resolve({ width, height });
        });
        img.addEventListener("error", () => {
            reject(new Error("There was some problem with the image."));
        });
    });
};
